package com.jfinal.bbsapp.common.task;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.bbsapp.common.kit.MyAnnotation;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

@MyAnnotation(memo="人脸核身定时获取access_token业务类")
public class FaceTaskService {
	
	private final Logger logger = Logger.getLogger(FaceTaskService.class);
	private static String appId = PropKit.get("appId"); //应用ID
	private static String secret = PropKit.get("secret"); //应用ID
	
	@MyAnnotation(memo="人脸核身获取access_token定时器")
	public void dotask() {
		String access_token= "";
		OkHttpClient client = new OkHttpClient();
		//https://kyc1.qcloud.com/api/oauth2/access_token?appId=TIDAeLsr&secret=553SMiJDqpIVrwIgciTrpr5ya5CKxK6D4swDOxn5mYFUcfQBUICqpOs9Sd7KtpBs&grant_type=client_credential&version=1.0.0
		String url = "https://kyc1.qcloud.com/api/oauth2/access_token?appId=" + appId + "&secret=" + secret + "&grant_type=client_credential&version=1.0.0";
		Request request = new Request.Builder().url(url).get().build();
		try (Response response = client.newCall(request).execute()) {
		    ResponseBody responseBody = response.body();
		    if (responseBody != null) {
		        String res = responseBody.string();
		        JSONObject resobj = JSON.parseObject(res);
		        String code = resobj.getString("code");
				if(code.equals("0")) {
					access_token = resobj.getString("access_token");
			        //System.out.println("access_token------>"+access_token);
			        Cache rds = Redis.use();
			        rds.set("wx-access_token", access_token); 
			        
			        String tickets= "";
					OkHttpClient client2 = new OkHttpClient();
					//https://kyc1.qcloud.com/api/oauth2/access_token?appId=TIDAeLsr&secret=553SMiJDqpIVrwIgciTrpr5ya5CKxK6D4swDOxn5mYFUcfQBUICqpOs9Sd7KtpBs&grant_type=client_credential&version=1.0.0
					String url2 = "https://kyc1.qcloud.com/api/oauth2/api_ticket?appId=" + appId + "&access_token=" + access_token + "&type=SIGN&version=1.0.0";
					Request request2 = new Request.Builder().url(url2).get().build();
					try (Response response2 = client2.newCall(request2).execute()) {
					    ResponseBody responseBody2 = response2.body();
					    if (responseBody2 != null) {
					        String res2 = responseBody2.string();
					        JSONObject resobj2 = JSON.parseObject(res2);
					        String code2 = resobj2.getString("code");
							if(code2.equals("0")) {
								JSONArray firstArray = resobj2.getJSONArray("tickets");
								JSONObject jsonObjecttickets = firstArray.getJSONObject(0);
								tickets = jsonObjecttickets.getString("value");
								//System.out.println("tickets------>"+tickets);
						        rds.set("wx-SIGN-tickets", tickets); 
							}
					    }
				    }catch (Exception e) {
						logger.error("Exception:" , e);	
					}
				}
		    }
	    }catch (Exception e) {
			logger.error("Exception:" , e);	
		}
	}

}
